package com.jiang.mall.mallorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiang.common.utils.PageUtils;
import com.jiang.common.utils.Query;
import com.jiang.mall.mallorder.dao.OrderItemDao;
import com.jiang.mall.mallorder.entity.OrderEntity;
import com.jiang.mall.mallorder.entity.OrderItemEntity;
import com.jiang.mall.mallorder.entity.OrderReturnReasonEntity;
import com.jiang.mall.mallorder.service.OrderItemService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.Map;

@RabbitListener(queues = {"hello-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

//    @RabbitListener(queues = {"hello-queue"})
    @RabbitHandler
    public void receiveMessage(Message message, OrderReturnReasonEntity content, Channel channel) {
        System.out.println("接收到消息内容：" + content );
        // channel内按属性自增的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag===>" + deliveryTag);
        // 签收货物，非批量模式
        try{
            if(deliveryTag % 2 == 0) {
                // 签收货物，非批量模式 第二个参数 multiple
                // 收货
                channel.basicAck(deliveryTag, false);
                System.out.println("签收了货物。。。" + deliveryTag);
            }else {
                // 第三个参数 requeue = false 丢弃， requeue = true 发回服务器，服务器消息入队
                channel.basicNack(deliveryTag, false, true);
                System.out.println("没有签收货物。。。" + deliveryTag);
            }
        }catch (Exception e) {
            // 网络中断
        }
    }

    @RabbitHandler
    public void receiveMessage2(Message message, OrderEntity content) {
//        System.out.println("接收到消息内容：" + message );
        System.out.println("对象二：" + content);
    }
}